<?php

namespace App\Exports;

use App\Models\Withdraw;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;

class XzbWithdrawExport 
{
	
	public function XzbExport()
    {
    	$list = $this->query();
 
        $newExcel = new Spreadsheet();  //创建一个新的excel文档
        $objSheet = $newExcel->getActiveSheet();  //获取当前操作sheet的对象
 
        //设置宽度为true,不然太窄了
        $newExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $newExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $newExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $newExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
 
        //设置第一栏的标题
        $objSheet->setCellValue('A1', '')
            ->setCellValue('B1', '')
            ->setCellValue('C1', '')
            ->setCellValue('D1', '');
        $objSheet->setCellValue('A2', '商户批次号（必填）')
            ->setCellValue('B2', '总笔数（非必填）')
            ->setCellValue('C2', '总金额（元，非必填）')
            ->setCellValue('D2', '');
         $objSheet->setCellValue('A3', time().rand(1000,9999))
            ->setCellValue('B3', '')
            ->setCellValue('C3', '')
            ->setCellValue('D3', '');
         $objSheet->setCellValue('A4', '商户订单号（非必填）')
            ->setCellValue('B4', '收款人姓名（必填）')
            ->setCellValue('C4', '身份证号（必填）')
            ->setCellValue('D4', '个人银行卡号（必填）')
			->setCellValue('E4', '付款金额（元，必填）')
			->setCellValue('F4', '手机号（必填）')
			->setCellValue('G4', '备注（非必填）');
 /*	$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);*/
        foreach ($list as $k => $val) {
            $k = $k + 5;
            $objSheet->setCellValue('A' . $k, $val['id'])
                ->setCellValue('B' . $k, $val['real_name'])
                ->setCellValueExplicit('C' . $k, $val['id_no'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING)
                ->setCellValueExplicit('D' . $k, $val['cert_number'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING)
                ->setCellValue('E' . $k, $val['actual_amount'])
                ->setCellValue('F' . $k, $val['phone'])
                ->setCellValue('G' . $k, '');
        }

        //dd($objSheet);die;
 
      	$this->downloadExcel($newExcel, time()."导出薪众包待提现数据", 'Xls');
    }


     //公共文件，用来传入xls并下载
    function downloadExcel($newExcel, $filename, $format)
    {
        // $format只能为 Xlsx 或 Xls
        if ($format == 'Xlsx') {
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        } elseif ($format == 'Xls') {
            header('Content-Type: application/vnd.ms-excel');
        }

 
        header("Content-Disposition: attachment;filename="
            . $filename . date('Y-m-d') . '.' . strtolower($format));
        header('Cache-Control: max-age=0');
        $objWriter = IOFactory::createWriter($newExcel, $format);
 
        $objWriter->save('php://output');

        exit;
    }



    public function query()
    {
    	return Withdraw::query()
    		->select('withdraws.*','users.real_name','users.cert_number','freelances.id_no','users.phone')
    		->join('users','users.id','withdraws.user_id')
    		->join('freelances','users.id','freelances.user_id')
		    ->where('withdraws.method','xzbpay')
		    ->where('withdraws.status','pending')
		    ->whereNotNull('cert_number')
		    ->get()->toArray();
    }
    
   
}
